package com.leicx.guli.cart.vo;

import cn.hutool.core.collection.CollUtil;
import lombok.Data;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author daxiong
 * @date 2022/8/3 下午3:05
 */
@Data
public class CartVo {
    private List<CartItemVo> items;
    /**
     * 商品数量
     */
    private Integer countNum;
    /**
     * 商品类型数量
     */
    private Integer countType;
    private BigDecimal totalAmount;
    private BigDecimal reduce = new BigDecimal("0.00");

    public Integer getCountNum() {
        countNum = 0;
        if (CollUtil.isNotEmpty(items)) {
            for (CartItemVo item : items) {
                if (item.getCheck()) {
                    countNum += item.getCount();
                }
            }
        }
        return countNum;
    }

    public Integer getCountType() {
        return CollUtil.isNotEmpty(items) ? 0 : items.size();
    }

    public BigDecimal getTotalAmount() {
        totalAmount = new BigDecimal(0);
        if (CollUtil.isNotEmpty(items)) {
            for (CartItemVo item : items) {
                if (item.getCheck()) {
                    totalAmount = totalAmount.add(item.getTotalPrice());
                }
            }
        }
        // 减去优惠总价
        return totalAmount.subtract(reduce);
    }
}
